'''
Ajax 抓取
url = https://spa1.scrape.center/
'''
import requests

url = 'https://spa1.scrape.center'

LIMIT = 10

idx_url = 'https://spa1.scrape.center/api/movie/?limit={limit}&offset={offset}'
detail_url = 'https://spa1.scrape.center/api/movie/{num}/'
# 这样是拿不到数据的
#response = requests.get(data_url)
# print(response.json())

def scrape_json_url(json_url):
    try:
        response = requests.get(json_url)
    except:
        print("出错了")
    else:
        return response.json()

# data = scrape_json_url(data_url)

# print(data)


def scrapy():
    for page in range(1,11):
        req_idx_url = idx_url.format(limit=LIMIT,offset=(page-1)*LIMIT)
        idx_resp_list = scrape_json_url(req_idx_url)
        #print(f"idx_resp_list = {idx_resp_list}")
        #print(f"idx_resp_list['results'] = {idx_resp_list['results']}")
        for idx_resp in idx_resp_list['results']:
            #print(idx_resp)
            detail_id = idx_resp['id']
            detail_req_url = detail_url.format(num=detail_id)
            detail_resp = scrape_json_url(detail_req_url)
            #print(detail_resp)    
            print(detail_resp['name'])
scrapy()